<template>
  <div>
    <!-- 索赔结算汇总提报 -->
    <TopFilter ref="TopFilterss" :list="topFilterInit" :cols="3" :collapse="collapse" labelWidth="110" @filterChange="changeHandle"></TopFilter>
    <!-- table -->
    <FilterTable
      ref="table"
      :columns="columns"
      :params="fetchParams"
      :fetchapi="fetchapi"
      :datakey="'records'"
      :onColumnsChange="columns => (this.columns = columns)"
      :onRowSelectChange="selectChange"
      :is-select-column="true"
      columnsRef="ClaimAccountSummarySubmitlgr"
    >
      <template slot="controls" slot-scope="props">
        <multiuse-button size="small" type="primary" icon="el-icon-plus" @click="add()">新增</multiuse-button>
        <multiuse-button size="small" type="primary" icon="el-icon-check" :click="qiyong.bind()">批量提交</multiuse-button>
        <multiuse-button size="small" type="primary" :click="exportFileBtn.bind()">导出</multiuse-button>
      </template>
    </FilterTable>
    <Drawer :visible.sync="visibleAdd" :title="'索赔结算汇总提报新增'" destroyOnClose width="60%" :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <AddForm @close="closeAdd" @save="saveAdd" type></AddForm>
    </Drawer>
    <Drawer :visible.sync="editFlag" :title="'索赔结算汇总提报编辑'" destroyOnClose width="60%" :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <EditForm @close="Editclose" @save="Editsave" :rowData="rowData"></EditForm>
    </Drawer>
    <Drawer :visible.sync="detailFlag" :title="'索赔结算汇总提报详情'" destroyOnClose width="60%" :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <DetailForm @close="detailclose" @save="detailsave" :rowData="rowData"></DetailForm>
    </Drawer>
  </div>
</template>
<script>
import AddForm from './AddForm.vue';
import EditForm from './EditForm.vue';
import DetailForm from './DetailForm.vue';
import { exportExcelFile } from '@/utils/index';
import { getListByPage, getPartType, getListForExport, deleteOne, quyrPSettleSum, editSave, submitById, piliangtijiao,exporturl } from '@/api/claim/factory/ClaimAccountSummarySubmit';
import { getAppId } from '@/assets/js/auth';
import { dictionary } from '@/utils/dictMixin';
import moment from 'moment';
export default {
  mixins: [dictionary],
  components: {
    AddForm,
    EditForm,
    DetailForm
  },
  data() {
    return {
      dataList: [],
      rowData: {},
      visible: false,
      detailFlag: false,
      editFlag: false,
      collapse: true,
      infoId: null,
      fetchapi: getListByPage,
      topFilterInit: this.topFilterInitFun(),
      flterValue: {}, //参数
      columns: this.creatHeader(),
      fetchParams: {
        xhrAbort: true,
        status: 1
      }, //ipa 接口
      selectedRows: [], //选中经销商
      dialogTitle: '选择车辆',
      visibleAdd: false
    };
  },
  created() {
    if (getAppId() == 'arcfox') {
      this.topFilterInit.find(x => x.fieldName == 'sumType').itemList = this.createDictList('5543', [55431001]);
    } else {
      this.topFilterInit.find(x => x.fieldName == 'sumType').itemList = this.createDictList('5543');
    }
    this.fetchParams.beginApplyDate = moment()
      .startOf('week')
      .format('YYYY-MM-DD HH:mm:ss');
    this.fetchParams.endApplyDate = moment()
      .endOf('week')
      .format('YYYY-MM-DD HH:mm:ss');
    this.chuqi();
    this.chuqi2();
  },
  methods: {
    //翻译
    async chuqi() {
      // const res = await getPartType({ type: 5547 });
      // delete res.data[1];
      // delete res.data[2];
      // delete res.data[3];
      // if (res.resultCode == 200) {
      //   this.topFilterInit.find((x) => x.fieldName == 'sumStatus').initialValue = '';
      //   this.topFilterInit.find((x) => x.fieldName == 'sumStatus').itemList = res.data.map((y) => ({ text: y.codeCnDesc, value: y.codeId }));
      // }
    },
    // 提交
    async submitFun(val) {
      this.$confirm('确定提交吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        this.res = await submitById(val.sumId);
        if (this.res.resultCode != 200) {
          this.$notify.error({ title: '提示信息', message: res.errMsg || '提交失败!' });
        } else {
          this.$notify.success({ title: '提示信息', message: '提交成功!' });
          this.fetchParams = { ...this.fetchParams };
        }
      });
    },
    //批量提交
    async qiyong() {
      if (this.selectedRows.length > 0) {
        let str = '';
        this.selectedRows.map(x => {
          str += x.sumId + ',';
        });
        console.log(str);
        const res = await piliangtijiao({ ids: str });
        this.fetchParams = { ...this.fetchParams };
      } else {
        return this.$notify.warning({ title: '提示信息', message: '请勾选后再进行提交！' });
      }
    },
    // 删除
    async delFun(val) {
      this.$confirm('确定删除吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        this.res = await deleteOne(val.sumId);
        if (this.res.resultCode != 200) {
          this.$notify.error({ title: '提示信息', message: res.errMsg || '删除失败!' });
        } else {
          this.$notify.success({ title: '提示信息', message: '删除成功!' });
          this.fetchParams = { ...this.fetchParams };
        }
      });
    },
    //索赔车辆
    async chuqi2() {
      // const res = await getPartType({ type: 5543 });
      // if(getAppId()=='arcfox') {
      //   delete res.data[0]
      // }
      // if (res.resultCode == 200) {
      //   this.topFilterInit.find((x) => x.fieldName == 'sumType').initialValue = '';
      //   this.topFilterInit.find((x) => x.fieldName == 'sumType').itemList = res.data.map((y) => ({ text: y.codeCnDesc, value: y.codeId }));
      // }
    },
    //选中
    selectChange(rows) {
      this.selectedRows = rows;
      console.log(this.selectedRows);
    },
    chooseOrderNo(val) {
      this.editFlag = true;
      this.rowData = val;
    },
    showDetailFun(val) {
      this.detailFlag = true;
      this.rowData = val;
    },
    // 导出
    async exportFileBtn() {
      let json = Object.assign({}, { CODE: 'CODE_00000146' }, this.fetchParams);
      let res = await exporturl(json);
      if (res.resultCode == 200) {
        this.$router.push('/admin/auth/oem/downloadCenter');
      }
      // let beginSubmitDate = this.topFilterInit.find(x => x.fieldName == 'beginApplyDate|endApplyDate').initialValue[0];
      // let endSubmitDate = this.topFilterInit.find(x => x.fieldName == 'beginApplyDate|endApplyDate').initialValue[1];
      // // if(!beginSubmitDate) {
      // //   this.$notify.warning({ title: '提示信息', message: '提交开始日期不可为空！' });
      // //   return
      // // }
      // // if(!endSubmitDate) {
      // //   this.$notify.warning({ title: '提示信息', message: '提交结束日期不可为空！' });
      // //   return
      // // }
      // if (getAppId() == 'arcfox') {
      //   if (moment(moment(endSubmitDate).format('YYYY-MM-DD')).diff(moment(beginSubmitDate), 'months', true) > 12) {
      //     this.$notify.warning({ title: '提示信息', message: '提交日期跨度不可超过十二个月！' });
      //     return;
      //   }
      // } else {
      //   if (moment(moment(endSubmitDate).format('YYYY-MM-DD')).diff(moment(beginSubmitDate), 'months', true) > 12) {
      //     this.$notify.warning({ title: '提示信息', message: '提交日期跨度不可超过十二个月！' });
      //     return;
      //   }
      // }
      // let obj = Object.assign({ status: 1 }, this.fetchParams, { xhrAbort: false });
      // obj.beginApplyDate = this.topFilterInit.find(x => x.fieldName == 'beginApplyDate|endApplyDate').initialValue[0];
      // obj.endApplyDate = this.topFilterInit.find(x => x.fieldName == 'beginApplyDate|endApplyDate').initialValue[1];
      // getListForExport(obj).then(res => {
      //   exportExcelFile(res.data, '索赔结算汇总单信息');
      // });
    },
    add() {
      this.visibleAdd = true;
    },
    closeAdd() {
      this.visibleAdd = false;
    },
    Editclose() {
      this.editFlag = false;
    },
    saveAdd() {
      this.visibleAdd = false;
      this.fetchParams = { ...this.fetchParams };
    },
    Editsave() {
      this.editFlag = false;
      this.fetchParams = { ...this.fetchParams };
    },
    //查看
    detailsave() {
      this.detailFlag = false;
      this.fetchParams = { ...this.fetchParams };
    },
    detailclose() {
      this.detailFlag = false;
    },
    changeHandle(val) {
      // if(!val.beginApplyDate) {
      //   this.$notify.warning({ title: '提示信息', message: '提交开始日期不可为空！' });
      //   return
      // }
      // if(!val.endApplyDate) {
      //   this.$notify.warning({ title: '提示信息', message: '提交结束日期不可为空！' });
      //   return
      // }
      if (val.beginApplyDate && val.endApplyDate) {
        if (getAppId() == 'arcfox') {
          if (moment(moment(val.endApplyDate).format('YYYY-MM-DD')).diff(moment(val.beginApplyDate), 'months', true) > 12) {
            this.$notify.warning({ title: '提示信息', message: '提交日期跨度不可超过十二个月！' });
            return;
          }
        } else {
          if (moment(moment(val.endApplyDate).format('YYYY-MM-DD')).diff(moment(val.beginApplyDate), 'months', true) > 12) {
            this.$notify.warning({ title: '提示信息', message: '提交日期跨度不可超过十二个月！' });
            return;
          }
        }
      }
      this.fetchParams = Object.assign({ status: 1 }, val, { xhrAbort: false });
    },
    //搜索数据
    topFilterInitFun() {
      return [
        { type: 'INPUT', label: '结算汇总单号', fieldName: 'sumNo', placeholder: '请输入结算汇总单号...', initialValue: '' },
        {
          type: 'RANGE_DATE',
          label: '提交时间',
          fieldName: 'beginApplyDate|endApplyDate',
          placeholder: '请输入',
          initialValue: [
            moment()
              .startOf('week')
              .format('YYYY-MM-DD HH:mm:ss'),
            moment()
              .endOf('week')
              .format('YYYY-MM-DD HH:mm:ss')
          ]
          // rules: [{ required: true, message: '请选择日期', trigger: 'change' }]
        },
        { type: 'SELECT', label: '状态', fieldName: 'sumStatus', placeholder: '请选择状态...', initialValue: '', itemList: this.createDictList('5547', [55471002, 55471003, 55471004]) },
        { type: 'SELECT', label: '索赔车辆属性', fieldName: 'sumType', placeholder: '请选择索赔车辆属性...', initialValue: '', itemList: [] }
      ];
    },
    //table表头和数据源
    creatHeader() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          fixed: 'left',
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          width: 150,
          dataIndex: 'column-action',
          render: (props, h) => {
            return (
              <div>
                <multiuse-button
                  size="mini"
                  type="text"
                  disabled={props.row.sumStatus == '55471002' || props.row.sumStatus == '55471003' || props.row.sumStatus == '55471004' ? true : false}
                  onClick={() => this.chooseOrderNo(props.row)}
                >
                  编辑
                </multiuse-button>
                <multiuse-button size="mini" type="text" onClick={() => this.showDetailFun(props.row)}>
                  查看
                </multiuse-button>
                <multiuse-button
                  size="mini"
                  type="text"
                  disabled={props.row.sumStatus == '55471002' || props.row.sumStatus == '55471004' || props.row.sumStatus == '55471005' ? true : false}
                  onClick={() => this.delFun(props.row)}
                >
                  删除
                </multiuse-button>
                <multiuse-button
                  size="mini"
                  type="text"
                  disabled={props.row.sumStatus == '55471002' || props.row.sumStatus == '55471003' || props.row.sumStatus == '55471004' ? true : false}
                  onClick={() => this.submitFun(props.row)}
                >
                  提交
                </multiuse-button>
              </div>
            );
          }
        },
        { title: '索赔车辆属性', dataIndex: 'sumTypeStr', sorter: true },
        { title: '结算汇总单号', dataIndex: 'sumNo', sorter: true },
        { title: '单据状态', dataIndex: 'statusStr', sorter: true },
        { title: '结算单数', dataIndex: 'settleNum', sorter: true },
        { title: '发票张数', dataIndex: 'invoiceSheets', sorter: true },
        { title: '结算总金额', dataIndex: 'invoiceAmount', sorter: true },
        { title: '索赔金额', dataIndex: 'claimSum', sorter: true },
        { title: '考核金额', dataIndex: 'assessSum', sorter: true },
        // { title: '正常索赔金额', dataIndex: 'zhengChangAmount', sorter: true },
        // { title: '免费保养金额', dataIndex: 'mianFeiAmount', sorter: true },
        // { title: '新车PDI金额', dataIndex: 'xinCheAmount', sorter: true },
        // { title: '技术支持金额', dataIndex: 'jiShuAmount', sorter: true },
        // { title: '服务活动金额', dataIndex: 'fuWuAmount', sorter: true },
        // { title: '让渡服务金额', dataIndex: 'rangDuAmount', sorter: true },
        { title: '提交时间', dataIndex: 'applyDate', sorter: true },
        { title: '审核时间', dataIndex: 'approveDate', sorter: true }
        // { title: '主键', dataIndex: 'sumId', hidden: true },
        // { title: '状态', dataIndex: 'sumStatus', hidden: true },
      ];
    }
  }
};
</script>
<style lang="less" scoped></style>
